﻿using Admin2024.Application.Contracts.User.Dto;
using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IRoleService;
using Admin2024.Domain.IDomainService.System.IUserService;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Application.Contracts.User
{
    public class UserInfoService : IUserInfoService
    {
        private readonly IUserProfileService userProfileService;

        private readonly IRoleProfileService roleProfileService;

        private readonly IUserRoleManagementService UserRoleManagementService;

        private readonly IRepository<AppUserRole> AppUserRoleRepository;

        
        private readonly IMapper mapper;
        public UserInfoService(IUserProfileService userProfileService,IMapper mapper, IRoleProfileService roleProfileService, IUserRoleManagementService userRoleManagementService, IRepository<AppUserRole> AppUserRoleRepository)
        {
            this.userProfileService=userProfileService;
            this.mapper =mapper;
            this.roleProfileService=roleProfileService;
            UserRoleManagementService=userRoleManagementService;
            this.AppUserRoleRepository = AppUserRoleRepository;
        }


        //获取指定用户
        public async Task<DomainResult<AppUserDto>> GetUserById(Guid Id)
        {
            var userData = await userProfileService.GetUserById(Id);
            
            var user = userData.Data;
            if (!userData.IsSuccess)
            {
                return DomainResult<AppUserDto>.Error(userData.Message);
            }
              
            var appUserDto = mapper.Map<AppUserDto>(user);
            return DomainResult<AppUserDto>.Success(appUserDto);
        }
        //根据token获取用户
        public async Task<DomainResult<UserInfoDto>> GetUserInfo(string token)
        {
            var userData = await userProfileService.GetUserInfo(token);
            var user = userData.Data;
            if (!userData.IsSuccess)
            {
                return DomainResult<UserInfoDto>.Error(userData.Message);
            }

            var appUserDto = mapper.Map<UserInfoDto>(user);
            return DomainResult<UserInfoDto>.Success(appUserDto);
        }
        //获取所有用户
        public async Task<DomainResult<List<AppUserDto>>> GetUsersWithPagination(int page, int pageSize)
        {
            var userList = await userProfileService.GetUsersWithPagination(page, pageSize);
           
            if (!userList.IsSuccess)
            {
                return DomainResult<List<AppUserDto>>.Error(userList.Message);
            }

            var userListDto = mapper.Map<List<AppUserDto>>(userList.Data);

            return DomainResult<List<AppUserDto>>.Success(userListDto);

        }


        public async Task<DomainResult<AppUserDto>> UpdateUserProfile(Guid Id,UserUpdateDto appUser)
        {
            var userData = await userProfileService.GetUserById(Id);
            var user = userData.Data;
            if (!userData.IsSuccess)
            {
                return DomainResult<AppUserDto>.Error(userData.Message);
            }


            var userUpdate = mapper.Map<AppUser>(appUser);
            var appUserD =  await userProfileService.UpdateUserProfile(Id, userUpdate);
            var appUserDto = mapper.Map<AppUserDto>(appUserD.Data);
            return DomainResult<AppUserDto>.Success(appUserDto);
        }
    }
}
